﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.Persistent;
using System.Data;
using Jumpton.Data;
using Jumpton.SFA.Server.Entity;

namespace Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures
{
    public class MeasuresGroupManager : BusinessLogic
    {
        public MEASURE_GROUP Load(int ID)
        {
            return (MEASURE_GROUP)ObjectRow.Load(typeof(MEASURE_GROUP), string.Format("this.ID={0}", ID));
        }

        //绑定判断是否有效
        public ObjectTable GetActiveYes(int Valid, decimal domainid)
        {
            StringBuilder sb = new StringBuilder();
            if(Valid >= 0)
            {
                sb.AppendFormat("this.ACTIVE='{0}'", Valid);
            }
            if(domainid > 0)
            {
                sb.AppendFormat("and this.DOMAINID='{0}'", domainid);
            }
            return new ObjectTable(typeof(MEASURE_GROUP), sb.ToString().TrimStart('a', 'n', 'd'));
        }

        public ObjectTable GetMeasuresGroupTable(decimal domainid, string active)
        {
            string strValid = string.Empty;
            if(!string.IsNullOrEmpty(active))
                strValid = " and ACTIVE='" + active + "'";
            return new ObjectTable(typeof(MEASURE_GROUP), "this.DOMAINID='" + domainid + "'" + strValid);

        }

        /// <summary>
        /// 供WebServiceV2使用的能根据updatetime增量下载数据。
        /// </summary>
        /// <param name="damainid"></param>
        /// <param name="userid"></param>
        /// <param name="updatetime"></param>
        /// <returns></returns>
        public ObjectTable GetMeasuresGroupTable(string damainid, string userid, string updatetime)
        {
            string sql = string.Format("this.DOMAINID={0} and this.LAST_UPDATE_TIME > '{1}' and "
            + "ID in (select distinct a.MEASURE_PROFILE_ID from CUSTOMERS a join route_customers b on a.ID = b.Customer_ID "
            + "where a.VALID = 1 and b.user_id = {2} and b.ROUTE_NUMBER=0)", damainid, updatetime, userid);

            return new ObjectTable(typeof(MEASURE_GROUP), sql);
        }

        public MEASURE_GROUP Load(decimal domainID, string code)
        {
            return (MEASURE_GROUP)ObjectRow.Load(typeof(MEASURE_GROUP), string.Format("this.DOMAINID='{0}' and this.CODE='{1}'", domainID, code));
        }




        public bool Save(MEASURE_GROUP measuregroup)
        {
            try
            {

                if(measuregroup.State == DataRowState.Added)
                {
                    measuregroup.CREATETIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
                }
                measuregroup.Save();

                return true;
            }
            catch(Exception ex)
            {
                measuregroup.Connection.RollbackTransaction();
                this.message = ex.Message;
                throw ex;
            }
        }

        public ObjectTable GetMeasureGroupList(string domainID, string updatetime)
        {
            return new ObjectTable(typeof(MEASURE_GROUP), "this.DOMAINID=" + domainID + " and this.LAST_UPDATE_TIME>'" + updatetime + "'");
        }

        public DataSet GetMeasuresTable(string domainID, string user_id, string updatetime)
        {
            string strSql = "";

            DataSet ds = SqlData.OpenSql(strSql);
            return ds;
        }

    }
}
